package com.huilian.tool.weixin.controller;

import com.huilian.tool.common.config.WeixinConfig;
import com.huilian.tool.weixin.dto.WeiXinDto;
import com.huilian.tool.weixin.enums.ButtonTypeEnum;
import com.huilian.tool.weixin.service.WeixinService;
import com.huilian.tool.common.utils.AesException;
import com.huilian.tool.weixin.vo.Button;
import com.huilian.tool.weixin.vo.ClickButton;
import com.huilian.tool.weixin.vo.Menu;
import lombok.extern.slf4j.Slf4j;
import org.dom4j.DocumentException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Map;

/**
 * @Author 少凡
 * @Date 2022-06-06 16:15
 * version 1.0
 */

@Slf4j
@RequestMapping("/api/weixin")
@Controller
public class WeixinController {

    @Resource
    private WeixinService weixinService;
    @Resource
    private WeixinConfig weixinConfig;

    /**
     * 微信开启开发者模式 配置接口
     *
     * @param request
     * @return
     */
    @GetMapping
    @ResponseBody
    public String openDeveloperModel(HttpServletRequest request) {
        return weixinService.openDeveloperModel(request, weixinConfig.getToken());
    }

    /**
     * 微信JsSdk配置
     *
     * @return
     */
    @ResponseBody
    @GetMapping("/wxJsSdkConfig")
    public Map<String, String> getWeiXinJsSdkConfig(WeiXinDto weiXinDto) {
        String jsApiTicket = weixinService.getJsApiTicket();
        return weixinService.getWeiXinJsSdkConfig(weiXinDto.getUrl(), jsApiTicket);
    }

    @ResponseBody
    @GetMapping("/getWxAccessToken")
    public String getWxAccessToken() {
        return weixinService.getAccessToken();
    }

    @ResponseBody
    @GetMapping("/getJsApiTicket")
    public String getJsApiTicket() {
        return weixinService.getJsApiTicket();
    }

    /**
     * 微信消息处理 return
     *
     * @param
     * @return
     */
    @PostMapping
    @ResponseBody
    public void messageManage(HttpServletRequest request) {
        try {
            weixinService.messageManage(request);
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (AesException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /**
     * 创建微信公众号菜单
     *
     * @return
     */
    @GetMapping("/create/menu")
    @ResponseBody
    public String createMenu() {
        Menu menu = initMenu();
        return weixinService.createMenu(weixinService.getAccessToken(), menu);
    }


    /**
     * 初始化菜单
     *
     * @return
     */
    private Menu initMenu() {
        Menu menu = new Menu();
        Button btn1 = getButton1();
        Button btn2 = getButton2();
//        Button btn3 = getButton3();
        menu.setButton(new Button[]{btn1, btn2});
        return menu;
    }

    /**
     * 运营登录菜单
     *
     * @return
     */
    private Button getButton1() {

        ClickButton button = new ClickButton();
        button.setName("运营登录");
        button.setKey("manage-login");
        button.setType(ButtonTypeEnum.VIEW.getCode());
//        button.setUrl(urlConfigProperties.getManageFrontEndUrl());
        return button;
    }

    /**
     * 运营注册菜单
     *
     * @return
     */
    private Button getButton2() {

        ClickButton button = new ClickButton();
        button.setName("代理登录");
        button.setKey("admin-login");
        button.setType(ButtonTypeEnum.VIEW.getCode());
//        button.setUrl(urlConfigProperties.getPcFrontEndUrl() + "/#/login");
        return button;
    }


    /**
     * 联系客服菜单
     *
     * @return
     */
    private Button getButton3() {

        ClickButton button = new ClickButton();
        button.setName("联系客服");
        button.setKey("customer-service");
        button.setType(ButtonTypeEnum.VIEW.getCode());
        button.setUrl("http://kefu.huilianpay.com/api/chat/wxLogin/wx2566acc8960c9249");
        return button;
    }


    @GetMapping("/openId")
    public String requestOpenId(String redirectUrl, String attach, HttpServletRequest request)
            throws UnsupportedEncodingException {
        return weixinService.getOpenId(redirectUrl, attach, request);
    }


    /**
     * 获取微信、支付宝OpenId出口
     *
     * @param request
     * @return
     */
    @GetMapping("/outPutOpenId")
    public String responseOpenId(HttpServletRequest request) {
        return weixinService.responseOpenId(request);
    }


    /**
     * 获取用户信息（先获取openID然后再获取用户信息）
     *
     * @param request
     * @return
     */
    @GetMapping("/outPutUserInfo")
    public String responseUserInfo(HttpServletRequest request) throws UnsupportedEncodingException {
        return weixinService.responseUserInfo(request);
    }
}
